home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / setattr.def < prev    next >
Encoding:
Text File  |  1994-05-31  |  6.3 KB  |  250 lines

  1. This file is setattr.def, from which is created setattr.c.
  2. It implements the builtins "export" and "readonly", in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES setattr.c
  23.  
  24. #include "../shell.h"
  25. #include "common.h"
  26. #include "bashgetopt.h"
  27.  
  28. extern int array_needs_making;
  29. extern char *this_command_name;
  30.  
  31. $BUILTIN export
  32. $FUNCTION export_builtin
  33. $SHORT_DOC export [-n] [-f] [name ...] or export -p
  34. NAMEs are marked for automatic export to the environment of
  35. subsequently executed commands.  If the -f option is given,
  36. the NAMEs refer to functions.  If no NAMEs are given, or if `-p'
  37. is given, a list of all names that are exported in this shell is
  38. printed.  An argument of `-n' says to remove the export property
  39. from subsequent NAMEs.  An argument of `--' disables further option
  40. processing.
  41. $END
  42.  
  43. /* For each variable name in LIST, make that variable appear in the
  44.    environment passed to simple commands.  If there is no LIST, then
  45.    print all such variables.  An argument of `-n' says to remove the
  46.    exported attribute from variables named in LIST.  An argument of
  47.   -f indicates that the names present in LIST refer to functions. */
  48. export_builtin (list)
  49.      register WORD_LIST *list;
  50. {
  51.   return (set_or_show_attributes (list, att_exported));
  52. }
  53.  
  54. $BUILTIN readonly
  55. $FUNCTION readonly_builtin
  56. $SHORT_DOC readonly [-n] [-f] [name ...] or readonly -p
  57. The given NAMEs are marked readonly and the values of these NAMEs may
  58. not be changed by subsequent assignment.  If the -f option is given,
  59. then functions corresponding to the NAMEs are so marked.  If no
  60. arguments are given, or if `-p' is given, a list of all readonly names
  61. is printed.  An argument of `-n' says to remove the readonly property
  62. from subsequent NAMEs.  An argument of `--' disables further option
  63. processing.
  64. $END
  65.  
  66. /* For each variable name in LIST, make that variable readonly.  Given an
  67.    empty LIST, print out all existing readonly variables. */
  68. readonly_builtin (list)
  69.      register WORD_LIST *list;
  70. {
  71.   return (set_or_show_attributes (list, att_readonly));
  72. }
  73.  
  74. /* For each variable name in LIST, make that variable have the specified
  75.    ATTRIBUTE.  An arg of `-n' says to remove the attribute from the the
  76.    remaining names in LIST. */
  77. int
  78. set_or_show_attributes (list, attribute)
  79.      register WORD_LIST *list;
  80.      int attribute;
  81. {
  82.   register SHELL_VAR *var;
  83.   int assign, undo = 0, functions_only = 0, any_failed = 0, opt;
  84.  
  85.   /* Read arguments from the front of the list. */
  86.   reset_internal_getopt ();
  87.   while ((opt = internal_getopt (list, "nfp")) != -1)
  88.     {
  89.       switch (opt)
  90.     {
  91.       case 'n':
  92.         undo = 1;
  93.         break;
  94.       case 'f':
  95.         functions_only = 1;
  96.         break;
  97.       case 'p':
  98.         break;
  99.       default:
  100.         builtin_error ("usage: %s [-nfp] [varname]", this_command_name);
  101.         return (EX_USAGE);
  102.     }
  103.     }
  104.   list = loptend;
  105.  
  106.   if (list)
  107.     {
  108.       if (attribute & att_exported)
  109.     array_needs_making = 1;
  110.  
  111.       while (list)
  112.     {
  113.       register char *name = list->word->word;
  114.  
  115.       if (functions_only)
  116.         {
  117.           var = find_function (name);
  118.           if (!var)
  119.         {
  120.           builtin_error ("%s: not a function", name);
  121.           any_failed++;
  122.         }
  123.           else
  124.         {
  125.           if (undo)
  126.             var->attributes &= ~attribute;
  127.           else
  128.             var->attributes |= attribute;
  129.         }
  130.           list = list->next;
  131.           if (attribute == att_exported)
  132.         array_needs_making++;
  133.           continue;
  134.         }
  135.  
  136.       assign = assignment (name);
  137.  
  138.           if (assign)
  139.         name[assign] = '\0';
  140.       if (legal_identifier (name) == 0)
  141.         {
  142.           builtin_error ("%s: not a legal variable name", name);
  143.           any_failed++;
  144.           list = list->next;
  145.           continue;
  146.         }
  147.  
  148.       if (assign)
  149.         {
  150.           name[assign] = '=';
  151.           /* This word has already been expanded once with command
  152.          and parameter expansion.  Call do_assignment_no_expand (),
  153.          which does not do command or parameter substitution. */
  154.           do_assignment_no_expand (name);
  155.           name[assign] = '\0';
  156.         }
  157.  
  158.       if (undo)
  159.         {
  160.           var = find_variable (name);
  161.           if (var)
  162.         var->attributes &= ~attribute;
  163.         }
  164.       else
  165.         {
  166.           SHELL_VAR *find_tempenv_variable (), *tv;
  167.  
  168.           if (tv = find_tempenv_variable (name))
  169.         {
  170.           var = bind_variable (tv->name, tv->value);
  171.           dispose_variable (tv);
  172.         }
  173.           else
  174.         var = find_variable (name);
  175.  
  176.           if (!var)
  177.         {
  178.           var = bind_variable (name, (char *)NULL);
  179.           var->attributes |= att_invisible;
  180.         }
  181.  
  182.           var->attributes |= attribute;
  183.         }
  184.  
  185.       array_needs_making++;    /* XXX */
  186.       list = list->next;
  187.     }
  188.     }
  189.   else
  190.     {
  191.       SHELL_VAR **variable_list;
  192.       register int i;
  193.  
  194.       if ((attribute & att_function) || functions_only)
  195.     {
  196.       variable_list = all_shell_functions ();
  197.       if (attribute != att_function)
  198.         attribute &= ~att_function;    /* so declare -xf works, for example */
  199.     }
  200.       else
  201.     variable_list = all_shell_variables ();
  202.  
  203.       if (variable_list)
  204.     {
  205.       for (i = 0; var = variable_list[i]; i++)
  206.         {
  207.           if ((var->attributes & attribute) && !invisible_p (var))
  208.         {
  209.           char flags[6];
  210.  
  211.           flags[0] = '\0';
  212.  
  213.           if (exported_p (var))
  214.             strcat (flags, "x");
  215.  
  216.           if (readonly_p (var))
  217.             strcat (flags, "r");
  218.  
  219.           if (function_p (var))
  220.             strcat (flags, "f");
  221.  
  222.           if (integer_p (var))
  223.             strcat (flags, "i");
  224.  
  225.           if (flags[0])
  226.             {
  227.               printf ("declare -%s ", flags);
  228.  
  229.               if (!function_p (var))
  230.             {
  231.               char *x = double_quote (value_cell (var));
  232.               printf ("%s=%s\n", var->name, x);
  233.               free (x);
  234.             }
  235.               else
  236.             {
  237.               char *named_function_string ();
  238.  
  239.               printf ("%s\n", named_function_string
  240.                   (var->name, function_cell (var), 1));
  241.             }
  242.             }
  243.         }
  244.         }
  245.       free (variable_list);
  246.     }
  247.     }
  248.   return (any_failed == 0 ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
  249. }
  250.